package BC;

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Hex;

import java.nio.charset.StandardCharsets;
import java.security.*;
import java.security.spec.ECGenParameterSpec;

public class sm2_demo {
    private static String M="lzc_SM2_demo";
    public static void main(String[] args) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, SignatureException {
        SM2Util sm2 = new SM2Util();
        KeyPairGenerator kpg=sm2.generator();
        KeyPair keyPair = kpg.generateKeyPair();
        PublicKey publicKey = keyPair.getPublic();
        PrivateKey privateKey = keyPair.getPrivate();
        String data = sm2.encrypt(publicKey,M);
        System.out.println("明文："+M);
        System.out.println("密文："+data);
        String text=sm2.decrypt(privateKey,data);
        System.out.println("解密结果："+text);


        // 签名原文
        byte[] plainText = "lzc_SM2_demo".getBytes(StandardCharsets.UTF_8);
        // 计算签名值
        byte[] signatureValue = sm2.sign(privateKey,plainText);
        System.out.printf("signature[%d]: %s\n", signatureValue.length, Hex.toHexString(signatureValue));

        // 验签
        System.out.println("Signature verify result: " + sm2.verify(publicKey,plainText,signatureValue));

    }


}
